package com.crape.topic.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crape.account.bo.IAccountBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.model.AccountInfo;
import com.crape.common.model.Article;
import com.crape.common.model.Subject;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.utils.PageUtils;
import com.crape.topic.bo.IArticleBO;
import com.crape.topic.bo.ISubjectBO;
import com.crape.topic.bo.ISubjectRefBO;

@Controller
public class SubjectController {

	private ISubjectBO subjectBOImpl;
	private IArticleBO articleBOImpl;
	private ISubjectRefBO topicRefBOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setsubjectBOImpl(ISubjectBO subjectBOImpl) {
		this.subjectBOImpl = subjectBOImpl;
	}

	@Autowired
	public void setarticleBOImpl(IArticleBO articleBOImpl) {
		this.articleBOImpl = articleBOImpl;
	}

	@Autowired
	public void setTopicRefBOImpl(ISubjectRefBO topicRefBOImpl) {
		this.topicRefBOImpl = topicRefBOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@RequestMapping("/topic/subjects.jsp")
	public String subjects(ModelMap modelMap, Integer pageNo) {
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long accountid = loginUser.getAccountid();
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Integer pageSize = 20;
		Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
		// 所有主题
		List<Subject> subjects = subjectBOImpl.getAllSubjects(accountid, startIndex, pageSize);
		if (subjects != null && subjects.size() % 2 == 1) {
			Subject last = new Subject();
			last.setRemark("last");
			subjects.add(last);
		}
		modelMap.put("allSubjects", subjects);
		modelMap.put("pageNo", pageNo);
		modelMap.put("pageNumArray", PageUtils.getPagedNumArray(subjectBOImpl.countAllSubjects(), pageSize));
		// 热门文章
		List<Article> hotArticles = articleBOImpl.getHotArticles();
		modelMap.put("hotArticles", hotArticles);
		return "/topic/subjects.html";
	}

	@RequestMapping("/topic/subject.jsp")
	public String subject(ModelMap modelMap, Long subjectid, Integer pageNo) {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Integer pageSize = 20;
		Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
		// 主题
		Subject subject = subjectBOImpl.getSubjectById(subjectid);
		// 是否关注了主题
		Long accountid = ThreadObjManager.getLoginUser().getAccountid();
		Integer isattention = subjectBOImpl.isSubjectAttention(accountid, subjectid);
		subject.setIsattention(isattention);
		modelMap.put("subject", subject);
		// 添加访问记录
		subjectBOImpl.addSubjectVisitor(accountid, subjectid);
		// 最近访问该主题的人
		List<AccountInfo> subjectVisitors = accountBOImpl.getTop16SubjectVisitors(subjectid);
		modelMap.put("subjectVisitors", subjectVisitors);
		// 该主题所有文章
		List<Article> Articles = articleBOImpl.getArticlesBySubjectid(subjectid, startIndex, pageSize);
		modelMap.put("articles", Articles);
		modelMap.put("subjectid", subjectid);
		modelMap.put("pageNo", pageNo);
		modelMap.put("pageNumArray", PageUtils.getPagedNumArray(articleBOImpl.countArticlesBySubjectid(subjectid), pageSize));
		// 热门文章
		List<Article> hotArticles = articleBOImpl.getHotArticles();
		modelMap.put("hotArticles", hotArticles);
		return "/topic/subject.html";
	}

	@RequestMapping("/topic/addSubjectAttention.json")
	public ModelAndView addSubjectAttention(Long subjectid) {
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long accountid = loginUser.getAccountid();
		ModelAndView mv = new ModelAndView();
		if(accountid == null){
			mv.addObject("flag", 1);
			mv.addObject("message", "请先登录或注册！");
			return mv;
		}
		try {
			topicRefBOImpl.addSubjectAttention(accountid, subjectid);
			mv.addObject("flag", 0);
			mv.addObject("message", "关注成功！");
			return mv;
		} catch (Exception e) {
			mv.addObject("flag", 1);
			mv.addObject("message", "关注失败");
			return mv;
		}
	}

	@RequestMapping("/topic/cancelSubjectAttention.json")
	public ModelAndView cancelSubjectAttention(Long subjectid) {
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long accountid = loginUser.getAccountid();
		ModelAndView mv = new ModelAndView();
		if(accountid == null){
			mv.addObject("flag", 1);
			mv.addObject("message", "请先登录或注册！");
			return mv;
		}
		try {
			topicRefBOImpl.cancelSubjectAttention(accountid, subjectid);
			mv.addObject("flag", 0);
			mv.addObject("message", "取消关注成功！");
			return mv;
		} catch (Exception e) {
			mv.addObject("flag", 1);
			mv.addObject("message", "取消关注失败");
			return mv;
		}
	}

}
